package resources.urlresource;

import org.junit.jupiter.api.Test;
import org.springframework.core.io.UrlResource;
import java.net.MalformedURLException;

//访问网络资源
public class UrlResourceDemo {
    @Test
    public void UrlResourceTest(){
        // http://前缀
        loadUrlResource("http://www.baidu.com");
        // file://前缀
        loadUrlResource("file:2023.3.9.txt");
    }

     //访问前缀是http的资源
    public static void loadUrlResource(String path) {
        //创建Resource接口实现类 即 URLResource
        try {
            UrlResource url = new UrlResource(path);
            //获取资源信息
            System.out.println(url.getFilename());
            System.out.println(url.getURL());
            System.out.println(url.getInputStream().read());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }


    }
}
